package com.youlu.campus.admin.order;

import com.youlu.campus.admin.auth.entity.SystemUser;
import com.youlu.campus.admin.auth.service.UserService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.ExcelUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.DeliveryOrderQueryVO;
import com.youlu.campus.entity.VO.req.DeliveryOrderRequestVO;
import com.youlu.campus.entity.VO.req.ImportDeliveryOrderExcel;
import com.youlu.campus.entity.VO.req.ImportExpressCompany;
import com.youlu.campus.service.order.impl.DeliveryOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 订单发货任务控制器
 */
@Slf4j
@RestController
public class DeliveryOrderTaskController {

    @Autowired
    private DeliveryOrderService deliveryOrderService;

    @Autowired
    private UserService userService;

    @GetMapping("/delivery-order/task/sync/{id}")
    public RestResult sync(@PathVariable String id){
        deliveryOrderService.sync(id);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/delivery-order/task/item/sync/{id}")
    public RestResult syncItem(@PathVariable String id){
        deliveryOrderService.syncItem(id);
        return RestResultBuilder.builder().success().build();
    }

    @GetMapping("/delivery-order/task/detail/{id}")
    public RestResult detail(@PathVariable String id){
        return RestResultBuilder.builder().data(deliveryOrderService.detail(id)).success().build();
    }

    @PostMapping("/express/list")
    public RestResult expressList(@RequestBody DeliveryOrderQueryVO query){
        return RestResultBuilder.builder().data(deliveryOrderService.expressList(query)).success().build();
    }

    @PostMapping("/delivery-order/task/list")
    public RestResult list(@RequestBody DeliveryOrderQueryVO query){
        return RestResultBuilder.builder().data(deliveryOrderService.list(query)).success().build();
    }

    @DeleteMapping("/delivery-order/task/remove/{id}")
    public RestResult remove(@PathVariable String id){
        deliveryOrderService.delete(id);
        return RestResultBuilder.builder().data(null).success().build();
    }

    @PostMapping("/delivery-order/item/list")
    public RestResult itemList(@RequestBody DeliveryOrderQueryVO query){
        return RestResultBuilder.builder().data(deliveryOrderService.itemList(query)).success().build();
    }

    /**
     * 发货信息导出
     * @param deliveryOrder
     * @return
     */
    @PostMapping("/delivery-order/item/export")
    public RestResult itemExport(@RequestBody DeliveryOrderQueryVO deliveryOrder){
        deliveryOrder.setOptUserId(AuthUtils.getUserId());
        SystemUser systemUser = userService.get(AuthUtils.getUserId());
        if (Objects.nonNull(systemUser)) {
            deliveryOrder.setOptUserName(systemUser.getLoginName());
            deliveryOrder.setOptUserRealName(systemUser.getRealName());
        }
        deliveryOrderService.exportItem(deliveryOrder);
        return RestResultBuilder.builder().data(null).success().build();
    }



    /**
     * 导入发货单
     * @param deliveryOrder
     * @param file
     * @param bindingResult
     * @return
     */
    @PostMapping("/delivery-order/import")
    public RestResult importDeliveryOrder(@Validated DeliveryOrderRequestVO deliveryOrder,
                                          @RequestParam("file") MultipartFile file, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        List<ImportDeliveryOrderExcel> excelDataList = ExcelUtils.readExcel(file, ImportDeliveryOrderExcel.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        deliveryOrderService.importDeliveryOrder(deliveryOrder,excelDataList);
        excelDataList.clear();
        return RestResultBuilder.builder().success().build();
    }
    @PostMapping("/express/import")
    public RestResult importExpress(@RequestParam("file") MultipartFile file){
        List<ImportExpressCompany> excelDataList = ExcelUtils.readExcel(file, ImportExpressCompany.class);
        if (CollectionUtils.isEmpty(excelDataList)) {
            return RestResultBuilder.builder().failure("导入文件错误").build();
        }
        deliveryOrderService.importExpressCompany(excelDataList);
        excelDataList.clear();
        return RestResultBuilder.builder().success().build();
    }






}
